package dragon.modules.po;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import dragon.core.db.DML;
import dragon.core.exception.ExceptionHandler;

public class SysUser extends DragonBase {
	public static String tableName = "sys.t_user";

	// //特有字段
	private String login_name; // 登录名
	private String login_pwd; // MD5加密32位
	private String user_name; // 姓名（不可修改）
	private String user_nickname; // 用户昵称
	private String user_email; // 用户email
	private String last_login_time; // 上次登录时间yyyymmddhh24miss
	private String last_login_ip; // 上次登录IP
	private String user_phone; // 用户手机号码

	/* 以下是运行时字段 */
	// 之所以不采用List<Object>的结构，是因为那样会被ejp当为关联表来处理而报错(数据表没建外键)
	private Map<String, Object>[] user_orgs;
	private Map<String, Object>[] user_groups;
	

	private String user_org_ids;
	private String user_org_names;
	private String user_org_idpaths;

	private String user_group_ids;
	private String user_group_names;

	private String user_menu_html;
	
	//TODO 从缓存里取用户菜单
	
	
	
	public String getUserOrgNames() throws ExceptionHandler {
		if (user_org_names == null)
			getUserOrgs();
		return user_org_names;
	}
	public String getUserOrgIds() throws ExceptionHandler {
		if (user_org_names == null)
			getUserOrgs();
		return user_org_ids;
	}
	public String getUserOrgIdpaths() throws ExceptionHandler {
		if (user_org_names == null)
			getUserOrgs();
		return user_org_idpaths;
	}

	public String getUserGroupIds() throws ExceptionHandler {
		if (user_group_ids == null)
			getUserGroups();
		return user_group_ids;
	}
	
	public String getUserGroupNames() throws ExceptionHandler {
		if (user_group_names == null)
			getUserGroups();
		return user_group_names;
	}

	public String getUserMenus() throws ExceptionHandler{
		if(user_menu_html == null)
			getUserMenuData();
		return user_menu_html;
	}
	
	
	
	@SuppressWarnings("unchecked")
	public Map<String, Object>[]  getUserOrgs() throws ExceptionHandler {
		if(user_orgs != null )
			return user_orgs;
		String sql = "select sys.t_org.id,sys.t_org.state,sys.t_org.created_by,sys.t_org.created_by_name,sys.t_org.created_time,sys.t_org.modified_by,sys.t_org.modified_by_name,sys.t_org.modified_time,sys.t_org.data_orgs,sys.t_org.data_org_paths,sys.t_org.data_groups,sys.t_org.data_group_paths,sys.t_org.ordernum,sys.t_org.memo,sys.t_org.pid,sys.t_org.depth,sys.t_org.org_code,sys.t_org.org_name,sys.t_org.id_path,sys.t_org.org_code_path,sys.t_org.org_name_path from sys.t_org left join sys.t_user_org on sys.t_org.id=sys.t_user_org.org_id where sys.t_user_org.user_id=? ";
		List<Map<String, Object>> list = DML.prepareQuery(sql, id);
		if (list != null) {
			user_orgs = new HashMap[list.size()];
			int index = 0;
			StringBuffer sb_user_org_ids = new StringBuffer(",");
			StringBuffer sb_user_org_names = new StringBuffer(",");
			StringBuffer sb_user_org_idpaths = new StringBuffer(",");
			for (Map<String, Object> map : list) {
				user_orgs[index++] = map;
				sb_user_org_ids.append(map.get("ID")).append(",");
				sb_user_org_names.append(map.get("ORG_NAME")).append(",");
				sb_user_org_idpaths.append(map.get("ID_PATH")).append(",");
			}
			list = null;
			user_org_ids = sb_user_org_ids.toString();
			sb_user_org_ids = null;
			user_org_names = sb_user_org_names.toString();
			sb_user_org_names = null;
			user_org_idpaths = sb_user_org_idpaths.toString();
			sb_user_org_idpaths = null;
		}
		return user_orgs;
	}

	@SuppressWarnings("unchecked")
	public Map<String, Object>[] getUserGroups()throws ExceptionHandler{
		if(user_groups != null)
			return user_groups;
		String sql = "select  sys.t_group.id,sys.t_group.state,sys.t_group.created_by,sys.t_group.created_by_name,sys.t_group.created_time,sys.t_group.modified_by,sys.t_group.modified_by_name,sys.t_group.modified_time,sys.t_group.data_orgs,sys.t_group.data_org_paths,sys.t_group.data_groups,sys.t_group.data_group_paths,sys.t_group.ordernum,sys.t_group.memo,sys.t_group.group_name,sys.t_group.group_alias from sys.t_group left join sys.t_user_group on sys.t_group.id=sys.t_user_group.group_id where sys.t_user_group.user_id = ?";
		List<Map<String, Object>> list = DML.prepareQuery(sql, id);
		if (list != null) {
			user_groups = new HashMap[list.size()];
			int index = 0;
			StringBuffer sb_user_group_ids = new StringBuffer(",");
			StringBuffer sb_user_group_names = new StringBuffer(",");
			for (Map<String, Object> map : list) {
				user_groups[index++] = map;
				sb_user_group_ids.append(map.get("ID")).append(",");
				sb_user_group_names.append(map.get("GROUP_NAME")).append(",");
			}
			list = null;
			user_group_ids = sb_user_group_ids.toString();
			sb_user_group_ids = null;
			user_group_names = sb_user_group_names.toString();
			sb_user_group_names = null;

		}
		return user_groups;
	}
	
	private void getUserMenuData() throws ExceptionHandler{
		
		
		//TDOD 绘制用户菜单
	}
	
	
	
	
	
	/**
	 * 判断用户是否拥有某种角色
	 * 
	 * @param groupID
	 *            角色id
	 * @return
	 * @throws ExceptionHandler
	 */
	public boolean inGroup(int groupID) throws ExceptionHandler {
		if (user_groups == null) {
			getUserGroups();
		}
		if (user_groups.length == 0) {
			return false;
		}
		for (Map<String, Object> ug : user_groups) {
			if (groupID == Integer.parseInt(ug.get("ID").toString())) {
				return true;
			}
		}
		return false;
	}

	public String getLogin_name() {
		return login_name;
	}

	public void setLogin_name(String login_name) {
		this.login_name = login_name;
	}

	public String getLogin_pwd() {
		return login_pwd;
	}

	public void setLogin_pwd(String login_pwd) {
		this.login_pwd = login_pwd;
	}

	public String getUser_name() {
		return user_name;
	}

	public void setUser_name(String user_name) {
		this.user_name = user_name;
	}

	public String getUser_nickname() {
		return user_nickname;
	}

	public void setUser_nickname(String user_nickname) {
		this.user_nickname = user_nickname;
	}

	public String getUser_email() {
		return user_email;
	}

	public void setUser_email(String user_email) {
		this.user_email = user_email;
	}

	public String getLast_login_time() {
		return last_login_time;
	}

	public void setLast_login_time(String last_login_time) {
		this.last_login_time = last_login_time;
	}

	public String getLast_login_ip() {
		return last_login_ip;
	}

	public void setLast_login_ip(String last_login_ip) {
		this.last_login_ip = last_login_ip;
	}

	public String getUser_phone() {
		return user_phone;
	}

	public void setUser_phone(String user_phone) {
		this.user_phone = user_phone;
	}


}
